varying vec3 pos;
varying vec2 texCoord;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D depth_map1;
uniform sampler2D depth_map2;
uniform sampler2D screen_noise;
uniform int num;
uniform float brightness;
uniform float flash;
uniform float blur;
uniform float rnd_x;
uniform float rnd_y;
uniform vec4 sun;
uniform float Width;
uniform float Height;
uniform int draw_info;
uniform int frames;
vec4 test[9];
bool test_inf[9];
vec2 off = vec2(1.0/Width, 1.0/Height);
float center_depth;
 
 vec2 calc_off(int i) {
 	if(i == 1) return off;
 	if(i == 2) return -off;
 	if(i == 3) return vec2(off.x,-off.y);
 	if(i == 4) return vec2(-off.x,off.y);
 	if(i == 5) return vec2(off.x,-off.y);
 	if(i == 6) return vec2(off.x,off.y);
 	if(i == 7) return vec2(-off.x,-off.y);
 	if(i == 8) return vec2(off.x,-off.y);
 }

vec4 antialliasing_1()
{
	for(int i = 1; i < 9; i++) {
		test_inf[i] = false;
	}	
	vec4 ret = texture2D(tex1,texCoord);
	test[0] = texture2D(depth_map1,texCoord);
	int pixel_count = 0;
	for(int i = 1; i < 9; i++) {
		test[i] = texture2D(depth_map1,texCoord+calc_off(i));
		test_inf[i] = true;
		if(test[i].x-test[0].x>=0.00005) {
			ret += texture2D(tex1,texCoord+calc_off(i));
			pixel_count++;
					
		}
		if(i == 4 && pixel_count == 0) return ret;//no chance
	}
	ret/=pixel_count+1;
	return ret;
}

vec4 antialliasing_2()
{
	vec4 ret = texture2D(tex2,texCoord);
	int pixel_count = 0;
	for(int i = 1; i < 9; i++) {
		if(!test_inf[i])test[i] = texture2D(depth_map1,texCoord+calc_off(i));
		if(test[i].x-test[0].x>=0.00005) {
			ret += texture2D(tex2,texCoord+calc_off(i));
			pixel_count++;
				
		}
		if(i == 4 && pixel_count == 0) return ret;//no chance
	}
	ret/=pixel_count+1;
	return ret;
}

vec4 god_rays_1() {	 
	 vec2 coor;//screen light pos
	 vec4 pos2 = gl_ModelViewProjectionMatrix * sun;
     coor.x = (pos2.x / pos2.w)/2.0 + 0.5;
     coor.y = (pos2.y / pos2.w)/2.0 + 0.5;
     vec2 delta = (coor -texCoord)/25.0;
     vec2 new_texCoord = texCoord;
     vec3 color = vec3(0.0);
     if(coor.x >= 1.0 || coor.x <= 0.0 || coor.y >= 1.0 || coor.y <= 0.0) return vec4(0.0);
     float gamma = sqrt(pow(abs(delta.x),2.0) + pow(abs(delta.y),2.0))*80.0;
     gamma = 1.0 - min(1.0, gamma);
     if(gamma == 0.0) return vec4(0.0);
     float minim = min(coor.x,coor.y);
     float maxim = max(coor.x,coor.y);
     maxim = 1.0 - maxim;
     float fresnel = min(maxim,minim);
     if(fresnel <= 0.33) fresnel*=3.0;
     else fresnel = 1.0;
     vec3 sample;
	 for(int i = 0; i < 25; i++) {
	 	if(num == 1) sample = texture2D(tex1,new_texCoord).xyz;
	 	else sample = texture2D(tex2,new_texCoord).xyz;
	 	sample*=3.0;
	 	sample-=vec3(1.0);
	 	sample.x = max(0.0,sample.x); sample.y = max(0.0,sample.y); sample.z = max(0.0,sample.z);
     	color+=sample*fresnel/100.0;
     	new_texCoord += delta;
     }
     
     return vec4(color,1.0)*gamma;
}

void main(void) {
	center_depth = texture2D(depth_map1,vec2(0.5,0.5)).x;
    vec4 color = vec4(0.0);
    color += god_rays_1();
    if(num == 1) {
    	color += texture2D(tex1,texCoord)*blur;//antialliasing_1()*blur;
    	color += texture2D(tex2,texCoord)*(1.0-blur);//antialliasing_2()*(1.0-blur);
    }
    else if(num == 2) {
    	color += texture2D(tex1,texCoord)*(1.0-blur);//antialliasing_1()*(1.0-blur);
    	color += texture2D(tex2,texCoord)*blur;//antialliasing_2()*blur;
    }
    
    gl_FragColor = color*brightness;
    gl_FragColor += vec4(flash);
    float x_offset = texCoord.x+rnd_x;
    float y_offset = texCoord.y+rnd_y;
    if(x_offset >1.0) x_offset -= 1.0;
    if(x_offset <0.0) x_offset += 1.0;
    if(y_offset >1.0) y_offset -= 1.0;
    if(y_offset <0.0) y_offset += 1.0;
    gl_FragColor += texture2D(screen_noise,vec2(x_offset,y_offset))/30.0;
    float eye_vision = (texCoord.x - 0.5)*(texCoord.x - 0.5) + (texCoord.y - 0.5)*(texCoord.y - 0.5);
    if(eye_vision > 0.09) {
    eye_vision += 0.41;
    eye_vision *= 2.0;
    gl_FragColor.xyz /= eye_vision;
    }
    float result_bright = (50.0 - float(frames))/50.0;
    if(draw_info == 1) {
    	gl_FragColor.x += 0.2*result_bright;
    	gl_FragColor.y += 0.2*result_bright;
    	gl_FragColor.z -= 0.4*result_bright;
    }
    if(draw_info == 2) {
    	gl_FragColor.x += 0.4*result_bright;
    	gl_FragColor.y -= 0.2*result_bright;
    	gl_FragColor.z -= 0.2*result_bright;
    }
    if(draw_info == 3) {
    	gl_FragColor.x -= 0.2*result_bright;
    	gl_FragColor.y += 0.4*result_bright;
    	gl_FragColor.z -= 0.2*result_bright;
    }
    if(draw_info == 4) {
    	gl_FragColor.x -= gl_FragColor.x*(float(frames)/50.0);
    	gl_FragColor.y -= gl_FragColor.y*(float(frames)/50.0);
    	gl_FragColor.z -= gl_FragColor.z*(float(frames)/50.0);
    }
    
}
